package org.aynu.propertyservice.controller;

import org.aynu.propertyservice.common.PropertyResponse;
import org.aynu.propertyservice.domain.RepairRequestsPo;
import org.aynu.propertyservice.service.IRepairRequestsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Timestamp;
import java.time.Instant;
import java.util.List;
import java.util.Map;

/**
 * 维修请求控制器
 *
 * @author 小粥
 * @since 2025/03/03
 */
@RestController
@RequestMapping("/repair-requests")
public class RepairRequestsController {

    private final IRepairRequestsService repairRequestsService;

    @Autowired
    public RepairRequestsController(IRepairRequestsService repairRequestsService) {
        this.repairRequestsService = repairRequestsService;
    }

    // 前端请求的 DTO
    public static class RepairRequestDTO {
        private Integer userId;
        private DescriptionDetail description;

        public Integer getUserId() {
            return userId;
        }

        public void setUserId(Integer userId) {
            this.userId = userId;
        }

        public DescriptionDetail getDescription() {
            return description;
        }

        public void setDescription(DescriptionDetail description) {
            this.description = description;
        }

        public static class DescriptionDetail {
            private String type;
            private String description;
            private String phone;

            public String getType() {
                return type;
            }

            public void setType(String type) {
                this.type = type;
            }

            public String getDescription() {
                return description;
            }

            public void setDescription(String description) {
                this.description = description;
            }

            public String getPhone() {
                return phone;
            }

            public void setPhone(String phone) {
                this.phone = phone;
            }
        }
    }

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public PropertyResponse<Boolean> save(@RequestBody RepairRequestDTO requestDTO) {
        RepairRequestsPo repairRequestsPo = new RepairRequestsPo();
        repairRequestsPo.setUserId(requestDTO.getUserId());
        RepairRequestDTO.DescriptionDetail desc = requestDTO.getDescription();
        repairRequestsPo.setType(desc.getType());
        repairRequestsPo.setDescription(desc.getDescription());
        repairRequestsPo.setPhone(desc.getPhone());
        repairRequestsPo.setStatus("pending");
        repairRequestsPo.setCreatedAt(new Timestamp(System.currentTimeMillis()));
        repairRequestsPo.setUpdatedAt(new Timestamp(System.currentTimeMillis()));

        return PropertyResponse.success(repairRequestsService.save(repairRequestsPo), "所有用户已存在该年份缴费记录");
    }

    @RequestMapping(value = "/selectResident", method = RequestMethod.POST)
    public PropertyResponse<List<RepairRequestsPo>> selectResident(@RequestBody Map<String, Object> map) {
        Integer userId = Integer.parseInt(map.get("userId").toString());
        return repairRequestsService.selectResident(userId);
    }

    @RequestMapping(value = "/selectAll", method = RequestMethod.POST)
    public PropertyResponse<Object> selectAll() {
        return PropertyResponse.success(repairRequestsService.list(), "所有用户已存在该年份缴费记录");
    }

    @RequestMapping(value = "/updateRequest", method = RequestMethod.POST)
    public PropertyResponse<Integer> update(@RequestBody Map<String, Object> map) {
        Integer requestId = Integer.valueOf(map.get("requestId").toString());
        String status = map.get("status").toString();
        Integer assignedTo = Integer.valueOf(map.get("assignedTo").toString());

        // 使用 Instant 获取当前时间并转换为 Timestamp 类型
        Timestamp resolvedAt = Timestamp.from(Instant.now());

        return repairRequestsService.updateRequest(requestId, status, assignedTo, resolvedAt);
    }
}